package com.guan.ordering_sys.generator.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guan.ordering_sys.common.PageResult;
import com.guan.ordering_sys.common.StateCode;
import com.guan.ordering_sys.exception.BaseException;
import com.guan.ordering_sys.generator.entity.Goods;
import com.guan.ordering_sys.generator.mapper.GoodsMapper;
import com.guan.ordering_sys.generator.service.CompanyGoodsService;
import com.guan.ordering_sys.generator.service.CompanyService;
import com.guan.ordering_sys.generator.service.GoodsService;
import com.guan.ordering_sys.param.GoodsUpdateParam;
import com.guan.ordering_sys.param.PageParam;
import com.guan.ordering_sys.vo.CompanyVo;
import com.guan.ordering_sys.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  GoodsServiceImpl 服务实现类
 * </p>
 *
 * @author 关远键
 * @version v1.0.2
 * @since 2021-06-18
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private CompanyGoodsService companyGoodsService;

    @Autowired
    private CompanyService companyService;

    /**
     * 根据商品编号获取商品详情
     * @author 关远键
     * @date 2021/6/23 19:40
     * @param id 商品名称
     * @return com.guan.ordering_sys.vo.GoodsVo
     */
    @Override
    public Goods getGoodsById(Long id) {
        Goods goods = goodsMapper.getGoodsById(id);
        if (goods == null) {
            throw new BaseException(StateCode.NOT_FOUND);
        }
        return goods;
    }

    /**
     * 根据商品类型编号获取商品列表
     * @author 关远键
     * @date 2021/6/23 19:41
     * @param goodsVoPage 分页
     * @param id 商品类型编号
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.guan.ordering_sys.vo.GoodsVo>
     */
//    @Override
//    public IPage<GoodsVo> getListByTypeId(Page<GoodsVo> goodsVoPage,Long id) {
//        IPage<GoodsVo> goodsVoIPage =  goodsMapper.getListByTypeId(goodsVoPage,id);
//
//        if (goodsVoIPage.getRecords().size() == 0) {
//            throw new BaseException(StateCode.FAIL);
//        }
//
//        return goodsVoIPage;
//    }

    /**
     * 分页查询商品列表
     * @author 关远键
     * @date 2021/6/27 14:06
     * @param pageParam
     * @return com.guan.ordering_sys.common.PageResult
     */
    @Override
    public PageResult getList(PageParam pageParam) {
        Page<GoodsVo> goodsVoPage = new Page<>();
        goodsVoPage.setCurrent(pageParam.getPageNum());
        goodsVoPage.setSize(pageParam.getPageSize());

        IPage<GoodsVo> page = goodsMapper.getList(goodsVoPage);
        if (page.getSize() == 0) {
            throw new BaseException(StateCode.NOT_FOUND);
        }

        //查询所属公司
        List<GoodsVo> records = page.getRecords();
        for (GoodsVo record : records) {
            Long companyId = companyGoodsService.getByGoodsId(record.getId());
            CompanyVo companyVo = companyService.getById(companyId);
            record.setCompanyVo(companyVo);
        }

        return new PageResult(page.getRecords().size(), page.getRecords());
    }

    /**
     * 添加商品
     * @author 关远键
     * @date 2021/6/23 19:44
     * @param param 商品信息
     * @param picture 图片地址
     * @return int
     */
    @Override
    public int insert(Goods param, MultipartFile picture) {
        Goods goods = goodsMapper.getGoodsById(param.getGoodsId());
        //商品已存在
        if (goods != null) {
            throw new BaseException(StateCode.BUSINESS_EXCEPTION);
        }

        if (picture != null) {
            param.setPicture(picture.toString());
        }

        int i = goodsMapper.insert(param);
        if (i > 0) {
            //设置商品所属公司
            companyGoodsService.insert(5, param.getGoodsId());
            return i;
        } else {
            throw new BaseException(StateCode.DAO_EXCEPTION);
        }
    }

    /**
     * 根据商品编号更新商品数据
     * @author 关远键
     * @date 2021/6/23 20:05
     * @param param 其他参数
     * @param picture 图片
     * @return int
     */
    @Override
    public int updateById(GoodsUpdateParam param, MultipartFile picture) {
        Goods goods = goodsMapper.getGoodsById(param.getId());

        log.info("更新前" + goods.toString());

        if (goods == null) {
            throw new BaseException(StateCode.NOT_FOUND);
        }

        if (param.getName() != null) {
            goods.setName(param.getName());
        }
        if (param.getPrice() != null) {
            goods.setPrice(param.getPrice());
        }
        if (param.getStatus() != null) {
            goods.setStatus(param.getStatus());
        }
        if (picture != null) {
            goods.setPicture(picture.toString());
        }

        int i = goodsMapper.updateById(goods);
        if (i > 0) {
            return i;
        } else {
            throw new BaseException(StateCode.DAO_EXCEPTION);
        }
    }

    /**
     * 根据商品编号删除商品
     * @author 关远键
     * @date 2021/6/23 20:08
     * @param id 商品编号
     * @return int
     */
    @Override
    public int deleteById(Long id) {
        Goods goods = goodsMapper.getGoodsById(id);

        if (goods == null) {
            throw new BaseException(StateCode.NOT_FOUND);
        }

        //删除与公司的关联
        companyGoodsService.deleteById(goods.getGoodsId());

        return goodsMapper.deleteById(goods.getGoodsId());

    }
}
